package com.b2c.ftysys.sevice.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.create.NewCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.b2c.ftysys.dao.OrderDAO;
import com.b2c.ftysys.dao.PdtorderDAO;
import com.b2c.ftysys.dao.ProductDAO;
import com.b2c.ftysys.domain.ExampleFactory;
import com.b2c.ftysys.domain.Product;
import com.b2c.ftysys.domain.ProductExample;
import com.b2c.ftysys.sevice.ExcelIptService;
import com.b2c.ftysys.sevice.OrderReviewService;
import com.b2c.ftysys.sevice.ProductService;
import com.b2c.ftysys.view.application.AppErrorException;

@Service
@RemoteProxy(creator = NewCreator.class, name = "productImportServices")
public class ProductServiceImpl implements ExcelIptService, ProductService {

	@Autowired
	private OrderDAO orderDao = null;
	@Autowired
	private PdtorderDAO pdtOrderDao = null;
	@Autowired
	private ProductDAO productDao = null;
	@Autowired
	private OrderReviewService review = null;

	public OrderReviewService getReview() {
		return review;
	}

	public void setReview(OrderReviewService review) {
		this.review = review;
	}

	public OrderDAO getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDAO orderDao) {
		this.orderDao = orderDao;
	}

	public PdtorderDAO getPdtOrderDao() {
		return pdtOrderDao;
	}

	public void setPdtOrderDao(PdtorderDAO pdtOrderDao) {
		this.pdtOrderDao = pdtOrderDao;
	}

	public ProductDAO getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDAO productDao) {
		this.productDao = productDao;
	}

	public String getProId(String skuNo) {
		ProductExample example = new ProductExample();
		example.createCriteria().andCmtpdtskuidEqualTo(skuNo);
		List<Product> products = this.getProductDao().selectByExample(example);
		String proId = "";

		if (products != null) {
			if (products.size() == 1) {
				proId = products.get(0).getPdtskuid();
			} else {
				boolean first = true;
				for (Product pro : products) {
					if (first) {
						first = false;
						proId = pro.getPdtskuid();
					} else {
						proId += "," + pro.getPdtskuid();
					}
				}
			}
		}

		return proId;
	}

	public String getCmtSku(String idNo) {

		ProductExample example = new ProductExample();
		example.createCriteria().andPdtskuidEqualTo(idNo);
		List<Product> products = this.getProductDao().selectByExample(example);
		String cmtSkuNo = "";

		if (products != null) {
			if (products.size() == 1) {
				cmtSkuNo = products.get(0).getCmtpdtskuid();
			} else {
				boolean first = true;
				for (Product pro : products) {
					if (first) {
						first = false;
						cmtSkuNo = pro.getCmtpdtskuid();
					} else {
						cmtSkuNo += "," + pro.getCmtpdtskuid();
					}
				}
			}
		}
		return cmtSkuNo;
	}

	public int ImportExcel(String fileAddr) throws AppErrorException {
		return 0;
	}

	public List<Product> getProductBySku(String sku) {
		ProductExample example = ExampleFactory.createProductExample();
		example.createCriteria().andCmtpdtskuidEqualTo(sku);
		return this.productDao.selectByExample(example);
	}

	public List<Product> getProductByOrderIdSku(String orderId, String sku) {
		ProductExample example = ExampleFactory.createProductExample();
		example.createCriteria().andCmtpdtskuidEqualTo(sku)
				.andCmtOrderIdEqualTo(orderId);
		return this.productDao.selectByExample(example);
	}

	public Product getProductByFtySku(String ftySku) {
		ProductExample example = ExampleFactory.createProductExample();
		example.createCriteria().andPdtskuidEqualTo(ftySku);

		List<Product> products = this.productDao.selectByExample(example);

		if (null == products || products.size() != 1)
			return null;
		else
			return products.get(0);
	}

	/**
	 * 通过条码获取工厂产品，多个产品则返回
	 * 
	 * @param pdtId
	 *            条码
	 * @return
	 */
	public Product getProductByPdtId(String pdtId) {
		ProductExample example = ExampleFactory.createProductExample();
		example.createCriteria().andCmtpdtidEqualTo(pdtId);

		example.setOrderByClause(" pdtId desc ");
		List<Product> products = this.productDao.selectByExample(example);

		if (null == products) {
			return null;
		} else if (products.size() != 1) {
			String productName = "";
			boolean first = true;
			for (Product product : products) {
				if (first) {
					productName += product.getPdtskuid();
					first = false;
				} else {
					productName += "," + product.getPdtskuid();
				}
			}
			Product pro = products.get(0);
			pro.setPdtskuid(productName);
			return pro;
		} else
			return products.get(0);
	}

	public void regRoute(String ftySku, String routeId) {
		Product product = this.getProductByFtySku(ftySku);

		if (null == product || StringUtils.isEmpty(routeId)
				|| !StringUtils.isEmpty(product.getRouteId()))
			return;

		product.setRouteId(routeId);
		product.setPdtStatus(Product.ON_PROCESS);
		this.productDao.updateByPrimaryKey(product);
	}

	public void updateRoute(String ftySku, String routeId) {
		Product product = this.getProductByFtySku(ftySku);
		if (null == product)
			return;
		product.setRouteId(routeId);
		product.setPdtStatus(Product.ON_SET);
		this.productDao.updateByPrimaryKey(product);
	}

	public void makeUp(String proSku) {
		updateStatusBySku(proSku, Product.DONE);
	}

	public void cancelProduct(String proSku) {
		updateStatusBySku(proSku, Product.CANCEL);
	}

	private void updateStatusBySku(String sku, int status) {
		Product product = this.getProductByFtySku(sku);
		if (null == product)
			return;
		if (Product.DONE == status) {
			product.setSendDate(new Date());
		}

		product.setPdtStatus(status);
		this.productDao.updateByPrimaryKey(product);
	}

	public List<Product> seachByCmtSku(String cmtSku) {
		ProductExample example = ExampleFactory.createProductExample();
		example.createCriteria().andPdtskuidLike("%" + cmtSku + "%");
		example.setPageSize(20);
		return this.productDao.selectByExample(example);
	}
}
